#!/usr/bin/python

class QuickFind(object):

  def __init__(self, num_objects):
    self.id_list = range(num_objects)

  def connected(self, p, q):
    return self.id_list[p] == self.id_list[q]

  def union(self, p, q):
    p_id = self.id_list[p]
    q_id = self.id_list[q]
    for pos, component_id in enumerate(self.id_list):
      if component_id == p_id:
        self.id_list[pos] = q_id

def main():
  num_objects = 10
  qf = QuickFind(num_objects)
  qf.union(3,4)
  qf.union(3,4)
  qf.union(4,9)
  print qf.connected(3,4)
  print 'pos: ', range(num_objects)
  print 'ids: ', qf.id_list

if __name__ == '__main__':
  main() 
